package com.bjpowernode.service.Impl;

import com.bjpowernode.form.RuleForm;
import com.bjpowernode.mapper.AdminMapper;
import com.bjpowernode.pojo.Admin;
import com.bjpowernode.service.AdminService;
import com.bjpowernode.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    AdminMapper adminMapper;

    @Override
    public ResultVO login(RuleForm ruleForm) {
        ResultVO resultVO = new ResultVO<>();
        Admin admin = adminMapper.selectByUserName(ruleForm.getUserName());
        if (admin == null) {
            resultVO.setCode(1);
        }else {
            if(admin.getPassword().equals(ruleForm.getPassWord())){
                resultVO.setCode(0);
                resultVO.setData(admin);
            }else {
                resultVO.setCode(2);
            }
        }return resultVO;
    }

    @Override
    public ResultVO enroll(RuleForm ruleForm) {
        Admin admin = adminMapper.selectByUserName(ruleForm.getUserName());
        ResultVO resultVO = new ResultVO<>();
        if(admin == null ){
            if(ruleForm.getPassWord().equals(ruleForm.getRepassword())) {
                adminMapper.insert(new Admin(ruleForm.getUserName(),ruleForm.getPassWord()));
                resultVO.setCode(0);
                resultVO.setData(admin);
            }else{
                resultVO.setCode(1);
            }

        }else{
            resultVO.setData(1);
        }
        return resultVO;
    }
}